package com.derek.omshopping.ui.mine;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.Toast;

import com.derek.omshopping.R;

public class NewAddressActivity extends AppCompatActivity {

    private LinearLayout area_location;
    private View pview;
    private PopupWindow popupWindow;
    private Spinner provinceSpinner = null;  //省级（省、直辖市）
    private Spinner citySpinner = null;     //地级市
    private Spinner countySpinner = null;    //县级（区、县、县级市）
    ArrayAdapter<String> provinceAdapter = null;  //省级适配器
    ArrayAdapter<String> cityAdapter = null;    //地级适配器
    ArrayAdapter<String> countyAdapter = null;    //县级适配器
    static int provincePosition = 1;
    private String province[] = new String[]{"北京", "上海", "天津", "重庆"};
    private String city[][] = new String[][]{{"东城区", "西城区", "崇文区", "宣武区", "朝阳区", "海淀区", "丰台区", "石景山区"},
            {"闵行区", "新城区", "松江区", "高新区", "朝阳区"}, {"滨海区", "崇文区", "惠济区"}, {"五菱", "岭南", "大足"}};

    private String country[][][] = new String[][][]{{{"东城区", "西城区", "崇文区", "宣武区", "朝阳区", "海淀区", "丰台区", "石景山区"}}, {{"闵行区", "新城区", "松江区", "高新区", "朝阳区"}},
            {{"滨海区", "崇文区", "惠济区"}}, {{"五菱", "岭南", "大足"}}};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_address);

        area_location = (LinearLayout) findViewById(R.id.area_location);
        popupWindow = new PopupWindow(pview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.update();
        popupWindow.setOutsideTouchable(true);
        pview = LayoutInflater.from(NewAddressActivity.this).inflate(R.layout.three_contract_layout, null);
        getSpinner();

    }


    private void getSpinner() {


        provinceSpinner = (Spinner) pview.findViewById(R.id.province_sp);
        citySpinner = (Spinner) pview.findViewById(R.id.city_sp);
        countySpinner = (Spinner) pview.findViewById(R.id.country_sp);
        provinceAdapter = new ArrayAdapter<String>(NewAddressActivity.this, android.R.layout.simple_spinner_item, province);
        provinceSpinner.setAdapter(provinceAdapter);
        cityAdapter = new ArrayAdapter<String>(NewAddressActivity.this, android.R.layout.simple_spinner_item, city[2]);
        citySpinner.setAdapter(cityAdapter);
        citySpinner.setSelection(0, true);
        countyAdapter = new ArrayAdapter<String>(NewAddressActivity.this, android.R.layout.simple_spinner_item, country[2][0]);
        countySpinner.setAdapter(countyAdapter);
        countySpinner.setSelection(0, true);
        /**
         * 监听省份变化
         */

        provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                cityAdapter = new ArrayAdapter<String>(NewAddressActivity.this, android.R.layout.simple_spinner_item, city[i]);
                citySpinner.setAdapter(cityAdapter);
                //记录当前位置
                provincePosition = i;

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        /**
         * 监听市的变化
         */


        countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                countyAdapter = new ArrayAdapter<String>(NewAddressActivity.this, android.R.layout.simple_spinner_item, country[provincePosition][i]);
                countySpinner.setAdapter(countyAdapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

    public void click(View view) {

        switch (view.getId()) {

            case R.id.pop_cancel:

                popupWindow.dismiss();
                break;

            case R.id.pop_sure:
                popupWindow.dismiss();
                break;
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (hasFocus){

            area_location.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Toast.makeText(NewAddressActivity.this,"wwwwwwwwwwwwwwww",Toast.LENGTH_LONG).show();

                    popupWindow.showAtLocation(pview, Gravity.BOTTOM, 0, 0);
                }
            });

        }
    }

}
